class Atividade < ActiveRecord::Base
	validates :disciplina, :presence => true, :uniqueness => {:case_sensitive => false}
	validates :codigo, :presence => true, :uniqueness => {:case_sensitive => false}
	validates :turma, :presence => true, :numericality => { :only_integer => true, :greater_than_or_equal_to => 1 }
	validates :professor, :presence => true, :uniqueness => {:case_sensitive => false}
	#validates :dias, :presence => true
	validates :horarioInicio, :presence => true
	validates :horarioTermino, :presence => true

	validates :salasdeaula, :presence => true
	validates :departamento, :presence => true

 	belongs_to :predio
  	belongs_to :salasdeaula


  	def self.ondeEstou?(id_sala)
  		if !id_sala.blank?
  			@sala = Salasdeaula.find(:first, :conditions => ["id = ?", id_sala])
  			@predio = Predio.find(:first, :conditions => ["id = ?", @sala.predio_id])
  			return [@sala, @predio]
  		end 
  	end

   	def self.searchDisciplina(query)
	    if !query.blank?
	    	find(:all, :conditions => ['codigo LIKE :query OR disciplina LIKE :query', {:query => "%#{query}%"}])
	    else
	    	all
	    end
 	end
   	def self.searchSalaporHorario(sala_id, horarioI, horarioT)
	    if !sala_id.blank?
			find(:all, :conditions => ['salasdeaula_id LIKE "%#{sala_id}%" and horarioInicio >= "%#{horarioI}%" and horarioTermino <= "%#{horarioT}%"'])	   
		else
	    	all
	    end
 	end

 	def self.searchProfessor(pro)
 		if !pro.blank?
			find(:all, :conditions => ['professor LIKE :pro', {:pro => "%#{pro}%"}], :order => 'professor')	   
		else
	    	all
	    end
 	end
end
